package com.offer;


import com.bean.TreeNode;
import com.utils.TreeUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Test34 {

    public List<List<Integer>> pathSum(TreeNode root, int target) {
        List<List<Integer>> result = new ArrayList<>();

        Stack<Integer> stack = new Stack<>();

        help(result, target, stack, root, 0);

        return result;
    }

    public void help(List<List<Integer>> result, int target, Stack<Integer> stack, TreeNode root, int sum) {
        if (null == root) {
            return;
        }

        sum += root.val;
        stack.push(root.val);

        if (sum == target && root.left == null && root.right == null) {
            result.add(new ArrayList(stack));
        }

        help(result, target, stack, root.left, sum);
        help(result, target, stack, root.right, sum);

        stack.pop();
    }

    public static void main(String[] args) {
        TreeNode rootNode = TreeUtils.buildTree();
        TreeUtils.showTree(rootNode);

        Test34 test34 = new Test34();
        List<List<Integer>> result = test34.pathSum(rootNode, 12);
        System.out.println(result);
    }

}
